package com.itheima.service;

import com.itheima.common.R;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.FlavorsMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Flavors;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Envy
 */
@Service
public class DishService {
    @Resource
    DishMapper dishMapper;
    @Resource
    FlavorsMapper flavorsMapper;

    /**
     * 菜品列表
     */
    public R<List<Dish>> selectDish(String categoryId, Integer status) {
        //1.查询菜品列表信息
        List<Dish> dishes = dishMapper.selectDish(categoryId, status);
        List<Dish> dishList = new ArrayList<>();

        //4.将查询到的菜品信息拷贝到dish对象中
        for (Dish dish1 : dishes) {
            Dish dish = new Dish();
            BeanUtils.copyProperties(dish1, dish);

            //2.根据dishid查询到对应的菜品口味列表信息
            List<Flavors> flavors = flavorsMapper.selectFlavors(dish.getId());

            //3.每查询到一个数据就将查询到的口味数据信息放到dish中的Flavors集合里
            dish.setFlavors(flavors);

            //重新把dish对象放到list集合中因为需要列表展示数据
            dishList.add(dish);
        }
        //5.返回新的dish集合
        return R.success(dishList);
    }
}
